#include "LoginWidget.h"

#include <QPainter>
#include <QSize>
#include <QApplication>

namespace UserManagementSystem {
LoginWidget::LoginWidget(QWidget* parent)
    :FramelessWidget(parent)
{
    //最大化，无边框
    this->showMaximized();

    this->setWindowTitle(QObject::tr("这是登录窗口"));

    m_mainLayout=new QVBoxLayout(this);
    m_btnsLayout=new QHBoxLayout();
    m_subLayout=new QHBoxLayout();

    m_closeBtn=new QPushButton();
    m_minimumBtn=new QPushButton();

    m_leftWidget=new UserManagementSystem::LeftLoginWidget();
    m_rightWidget=new UserManagementSystem::RightWidght();

    this->SetDefaultLayout();

    //绑定信号槽
    connect(m_minimumBtn,&QPushButton::clicked,this,&LoginWidget::On_MinimumBtn_Clicked);
    connect(m_closeBtn,&QPushButton::clicked,this,&LoginWidget::On_CloseBtn_Clicked);


}

void LoginWidget::SetDefaultLayout()
{
    QString background=QApplication::applicationDirPath()+"/res/Login/background.png";
    m_rightWidget->LoadBackGround(background);

    //主布局留出 左 上 右 下 边框
    // m_mainLayout->setContentsMargins(20+5,30+10,20+5,20);

    m_btnsLayout->addWidget(m_minimumBtn);
    m_btnsLayout->addWidget(m_closeBtn);
    m_btnsLayout->setAlignment(Qt::AlignRight);


    //左右窗口平分
    m_subLayout->setContentsMargins(25,0,25,20);
    m_subLayout->addWidget(m_leftWidget,1);
    m_subLayout->addSpacing(10);//固定宽度
    m_subLayout->addWidget(m_rightWidget,1);


    m_mainLayout->addLayout(m_btnsLayout);
    m_mainLayout->addLayout(m_subLayout);

    SetDefaultStyle();
}

void LoginWidget::SetDefaultStyle()
{
    LodeIcon();
    m_minimumBtn->setIcon(m_minIcon);
    m_minimumBtn->setIconSize(QSize(30,30));

    m_closeBtn->setIcon(m_closeIcon);
    m_closeBtn->setIconSize(QSize(30,30));

    QString btn_style=R"(
    QPushButton {
        background-color: transparent;
        border: none;
    }
    QPushButton:checked {
        background-color: transparent;
    }
)";
    m_minimumBtn->setStyleSheet(btn_style);

    m_closeBtn->setStyleSheet(btn_style);

}

void LoginWidget::paintEvent(QPaintEvent *event)
{
    //窗口区域绘制圆角，其他区域绘制阴影
    QPainter painter(this);

    //设置抗锯齿
    painter.setRenderHint(QPainter::Antialiasing);

    //先把整个窗口绘制为白色
    painter.fillRect(this->rect(),QColor(255,255,255,255));
}

void LoginWidget::LodeIcon()
{
    QString close_path=QApplication::applicationDirPath()+"/res/Login/close.png";
    QString min_path=QApplication::applicationDirPath()+"/res/Login/min.png";


    m_minIcon.addFile(min_path);
    m_closeIcon.addFile(close_path);

    if(!m_minIcon.isNull()&&!m_closeIcon.isNull()){
        qDebug()<<"icon都存在";
    }


}

void LoginWidget::On_MinimumBtn_Clicked()
{
    qDebug()<<"最小化窗口";
    this->showMinimized();
}

void LoginWidget::On_CloseBtn_Clicked()
{
    qDebug()<<"关闭窗口";
    this->close();
}
}

